package org.xhnews.ui;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import org.xhnews.bo.AuthenticationBo;
import org.xhnews.constant.Constant;

public class RegisterPopupScreen extends PopupScreen {
	EditField _edit_sn;
	public RegisterPopupScreen() {
		super(new VerticalFieldManager(), Field.FOCUSABLE);
		_edit_sn = new EditField("授权码: ", "", 10, EditField.FILTER_DEFAULT);
		add(_edit_sn);
		HorizontalFieldManager buttons = new HorizontalFieldManager(
				Field.FIELD_HCENTER | Field.USE_ALL_WIDTH);
		ButtonField submitButton = new ButtonField("注册");
		buttons.add(submitButton);
		submitButton.setChangeListener(sbcl);
		ButtonField cancelButton = new ButtonField("取消");
		buttons.add(cancelButton);
		cancelButton.setChangeListener(cbcl);
		add(buttons);
		add(new SeparatorField());
		
	}

	FieldChangeListener sbcl = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
//			Dialog dlg = new Dialog("wait please...", new String[] { "OK" }, null,
//					Dialog.OK, Bitmap.getPredefinedBitmap(Bitmap.HOURGLASS));
//			dlg.doModal(); 
			int responseCode=-1;
			if(!_edit_sn.getText().equals("")){
			responseCode=AuthenticationBo.regiser(_edit_sn.getText());
			}
			if(responseCode==Constant.RES_REGISTRATION_SUCC)
				close();
			else{				
				Dialog.alert(translateRspCode(responseCode));
			}
		}
	};
	FieldChangeListener cbcl = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			close();
		}
	};
	public String translateRspCode(int errCode){
		switch (errCode) {
		case Constant.RES_UNAUTHORIZED:
			return new String("此手机未授权!");
		case Constant.RES_DISABLED:
			return new String("此手机被禁用!");
		case Constant.RES_OVERDUE:
			return new String("服务已过期!");
		case Constant.RES_REGISTER_BY_OTHER_DEVICE:
			return new String("此手机号码已与其他手机绑定!");
		default:
			return new String("未知错误!");
		}
		
	}
}
